package com.ronipl.createtabs;



import profileactivity.AccountProfileActivity;


import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import approvalactivity.ApproveDialog;
//import android.app.Activity;
//import android.view.Menu;
//import android.view.MenuItem;

public class MainActivity extends FragmentActivity implements TabListener {

	ViewPager viewPager;
	ActionBar actionBar;
	Spinner filterformspinner;
	Context context;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_main);

		/*
		 * Typeface cus_font = Typeface.createFromAsset(this.getAssets(),
		 * "fonts/opensansregular.ttf"); TextView filterforms =
		 * (TextView)findViewById(R.layout.activity_main);
		 * filterforms.setTypeface(cus_font);
		 */

		viewPager = (ViewPager) findViewById(R.id.pager);
		viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				actionBar.setSelectedNavigationItem(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				/*
				 * if(arg0 == ViewPager.SCROLL_STATE_IDLE){ //Log.d("RONI",
				 * "onPageScrollStateChanged Idle"); }
				 * 
				 * if(arg0 == ViewPager.SCROLL_STATE_DRAGGING){ //Log.d("RONI",
				 * "onPageScrollStateChanged DRAGGING"); }
				 * 
				 * if(arg0 == ViewPager.SCROLL_STATE_SETTLING){ //Log.d("RONI",
				 * "onPageScrollStateChanged SETTLING"); }
				 */
			}
		});

		ImageView imgHome = new ImageView(this);
		imgHome.setImageResource(R.drawable.homel);

		ImageView imgMyapproval = new ImageView(this);
		imgMyapproval.setImageResource(R.drawable.myapproval);

		ImageView imgMyrequest = new ImageView(this);
		imgMyrequest.setImageResource(R.drawable.myrequestl);

		ImageView imgStarred = new ImageView(this);
		imgStarred.setImageResource(R.drawable.starredl);

		ImageView imgMypending = new ImageView(this);
		imgMypending.setImageResource(R.drawable.outboxl);

		actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		ActionBar.Tab tab1 = actionBar.newTab();
		tab1.setTabListener(this);
		// tab1.setText("Tab 1");
		tab1.setCustomView(imgHome);

		ActionBar.Tab tab2 = actionBar.newTab();
		tab2.setTabListener(this);
		// tab2.setText("Tab 2");
		tab2.setCustomView(imgMyapproval);

		ActionBar.Tab tab3 = actionBar.newTab();
		tab3.setTabListener(this);
		tab3.setCustomView(imgMyrequest);
		// tab3.setText("Tab 3");

		ActionBar.Tab tab4 = actionBar.newTab();
		tab4.setTabListener(this);
		tab4.setCustomView(imgStarred);
		// tab3.setText("Tab 4");

		ActionBar.Tab tab5 = actionBar.newTab();
		tab5.setTabListener(this);
		tab5.setCustomView(imgMypending);
		// tab3.setText("Tab 5");

		actionBar.addTab(tab1);
		actionBar.addTab(tab2);
		actionBar.addTab(tab3);
		actionBar.addTab(tab4);
		actionBar.addTab(tab5);

		// Creating a spinner position to top left of layout
		int titleId = Resources.getSystem().getIdentifier("action_bar_title",
				"id", "android");
		// attach listener to this spinner view for handling spinner selection
		// change
		View titleView = findViewById(titleId);
		// source of viewgrouputils class is given at the end of this post.
		Spinner spinnerView = (Spinner) getLayoutInflater().inflate(
				R.layout.spinner_layout, null);
		ViewGroupUtils.replaceView(titleView, spinnerView);
		addListenerOnItemSelection();

	}

	/*
	 * public void filterFormsSpinner(){ Spinner spinnerView =
	 * (Spinner)findViewById(R.layout.spinner_layout); List<String>
	 * filterFormsSpinner = new ArrayList<String>();
	 * 
	 * filterFormsSpinner.add("All forms");
	 * filterFormsSpinner.add("Leave request form");
	 * filterFormsSpinner.add("Maintenance form");
	 * filterFormsSpinner.add("Other form");
	 * 
	 * ArrayAdapter<String> filterFormsSpinnerAdapter = new
	 * ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
	 * filterFormsSpinner);
	 * filterFormsSpinnerAdapter.setDropDownViewResource(android
	 * .R.layout.simple_spinner_dropdown_item);
	 * spinnerView.setAdapter(filterFormsSpinnerAdapter); }
	 */

	Spinner spinnerView;

	public void addListenerOnItemSelection() {
		spinnerView = (Spinner) findViewById(R.id.filterformsspinner);
		spinnerView
				.setOnItemSelectedListener(new CustomOnItemSpinnerSelectedListener());
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// Log.d("RONI",
		// "onTabSelected at "+" position "+tab.getPosition()+"name"+tab.getText());
		viewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// Log.d("RONI",
		// "onTabUnselected at "+" position "+tab.getPosition()+"name"+tab.getText());

	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// Log.d("RONI",
		// "onTabReselected at "+" position "+tab.getPosition()+"name"+tab.getText());

	}

	// For choosing form

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		/*
		 * int id = item.getItemId(); if (id == R.id.action_settings) { return
		 * true; }
		 */

		switch (item.getItemId()) {

		case R.id.maintenance:
			Toast.makeText(this, "You clicked the maintenance button",
					Toast.LENGTH_LONG).show();
			return true;
		case R.id.payroll:
			Toast.makeText(this, "You clicked the payroll button",
					Toast.LENGTH_LONG).show();
			return true;
		case R.id.leave_request:
			Toast.makeText(this, "You clicked the leave request button",
					Toast.LENGTH_LONG).show();
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			// Toast.makeText(getBaseContext(), "Keycode menu clicked",
			// Toast.LENGTH_SHORT).show();
			showActivity();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void showActivity() {
		startActivity(new Intent(MainActivity.this, AccountProfileActivity.class));
	}
	
	public void showApproveDialog(View view){
		
		
		Toast.makeText(MainActivity.this, "from main Activity  Approve button", Toast.LENGTH_SHORT).show();
		/*ApproveDialog approveDialog = new ApproveDialog();
		approveDialog.show(getFragmentManager(), "ApproveDialog");*/
	}
}

class MyAdapter extends FragmentPagerAdapter {

	public MyAdapter(FragmentManager fm) {
		super(fm);
	}

	@Override
	public Fragment getItem(int arg0) {
		Fragment fragment = null;

		if (arg0 == 0) {
			fragment = new FragmentA();
		}

		if (arg0 == 1) {
			fragment = new FragmentB();
		}

		if (arg0 == 2) {
			fragment = new FragmentC();
		}

		if (arg0 == 3) {
			fragment = new FragmentD();
		}

		if (arg0 == 4) {
			fragment = new FragmentE();
		}

		return fragment;
	}

	@Override
	public int getCount() {
		return 5;
	}

}
